<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('参股基金月度分配') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('在管基金')" prop="fundId">
        <MainBodySelect v-model="queryParams.fundId" :show-label="false" parameters="fund" :scope-authority="true" />
      </el-form-item>
      <el-form-item :label="$t('周期')" prop="yearMonth">
        <el-date-picker
          v-model="queryParams.yearMonth"
          :clearable="false"
          type="month"
          value-format="yyyy-MM"
          :placeholder="$t('周期')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dealBfMonthlyAllocationList" :span-method="objectSpanMethod" :show-summary="true" :summary-method="getSummaries" tooltip-effect="darkTable" border>
      <el-table-column :label="$t('序号')" type="index" align="center" width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金/管理公司名称')" align="center" prop="dealName" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id: scope.row.dealId, companyId: scope.row.companyId, gpId: scope.row.gpId, dealType: 'bf'}}"
            class="link_a"
          >{{ scope.row.companyName }}</router-link>
        </template>
      </el-table-column>

      <el-table-column :label="$t('收益主体')" align="center" prop="incomeSubjectName" show-overflow-tooltip min-width="200" />
      <el-table-column :label="$t('分红/退出时间')" align="center" prop="distributionDate" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.distributionDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('分配金额') + this.$t('金额单位')" align="center" prop="distributionAmount" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其中：本金') + this.$t('金额单位')" align="center" prop="distributionPrincipal" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionPrincipal, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其中：收益') + this.$t('金额单位')" align="center" prop="distributionIncome" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionIncome, 'w') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { dealBfMonthlyAllocationList, getDealBfMonthlyAllocationSum } from '@/api/sass/dfgz/dfgzReport'
import MainBodySelect from '@/components/MainBodySelect'
export default {
  name: 'DealBfMonthlyAllocationView',
  components: {
    MainBodySelect
  },
  data() {
    return {
      dealBfMonthlyAllocationList: [],
      dealBfMonthlyAllocationSum: {},
      total: 0,
      loading: false,
      queryParams: {
        fundId: undefined,
        yearMonth: this.getNowFormatDate(),
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      dealBfMonthlyAllocationList(this.queryParams).then(response => {
        getDealBfMonthlyAllocationSum(this.queryParams).then(res => {
          this.dealBfMonthlyAllocationSum = res.data
        })
        this.dealBfMonthlyAllocationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    // 获取当前日期函数
    getNowFormatDate() {
      const date = new Date()
      const year = date.getFullYear() // 获取完整的年份(4位)
      let month = date.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
      // let strDate = date.getDate() // 获取当前日(1-31)
      // if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
      return `${year}-${month}`
    },
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/dfgz/report/getDealBfMonthlyAllocationList/export', this.queryParams)
      }).catch(() => {
      })
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex <= 1) {
        const spanArr = this.rowspan()
        for (let i = 0; i < spanArr.length; i++) {
          const _row = spanArr[rowIndex]
          return {
            rowspan: _row,
            colspan: 1
          }
        }
      }
    },
    rowspan() {
      // 二维数组，用于存放单元格合并规则
      const spanArr = []
      // 用于存储相同项的开始index
      let position = 0
      this.dealBfMonthlyAllocationList.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1)
          position = 0
        } else {
          if (this.dealBfMonthlyAllocationList[index].dealId === this.dealBfMonthlyAllocationList[index - 1].dealId) {
            spanArr[position] += 1 // 有相同项
            spanArr.push(0) // 名称相同后往数组里面加一项0
          } else {
            spanArr.push(1) // 同列的前后两行单元格不相同
            position = index
          }
        }
      })
      return spanArr
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 4) {
          sums[index] = this.moneyFormat(this.dealBfMonthlyAllocationSum.distributionAmount, 'w')
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.dealBfMonthlyAllocationSum.distributionPrincipal, 'w')
        } else if (index === 6) {
          sums[index] = this.moneyFormat(this.dealBfMonthlyAllocationSum.distributionIncome, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
